package com.jt.common.thread;

/**
 * 如何保证如下类的设计在外界能获取一个实例==>单例实例
 * */
class Singleton{
    private Singleton(){}
    private static Singleton instance;
    public static Singleton getInstance(){
        if (instance==null){
            synchronized (Singleton.class){
                if(instance==null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

public class SingleTests {
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                System.out.println(Singleton.getInstance());
            }
        };Thread t2 = new Thread(){
            @Override
            public void run() {
                System.out.println(Singleton.getInstance());
            }
        };Thread t3 = new Thread(){
            @Override
            public void run() {
                System.out.println(Singleton.getInstance());
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}
